#include "R_TableViewModel.h"

#include <QDebug>
#include <QIcon>
R_TableViewModel::R_TableViewModel(QObject* parent)
    : QAbstractTableModel{parent}
{
    /** 表头名称 */
    _header << "封面"
            << "学号"
            << "姓名"
            << "IC卡号"
            << "联系方式"
            << "状态"
            << "有效期至"
            << "操作";


    /** 测试数据 */

    // 有效数据样例
    QStringList data0;
    data0 << "202301001"
          << "张三"
          << "A1B2C3D4E5"
          << "13800138000"
          << "有效"
          << "2026-12-31";

    // 过期数据样例
    QStringList data1;
    data1 << "202302045"
          << "李四"
          << "F6G7H8I9J0"
          << "15912345678"
          << "已过期"
          << "2024-03-31";

    // 未绑定IC卡样例
    QStringList data2;
    data2 << "202303128"
          << "王五"
          << "N/A"
          << "17600001111"
          << "未激活"
          << "——";

    // 挂失状态样例
    QStringList data3;
    data3 << "202304067"
          << "赵六"
          << "K1L2M3N4O5"
          << "18833445566"
          << "已挂失"
          << "2025-09-30";

    // 国际学生样例
    QStringList data4;
    data4 << "G2023001"
          << "John Smith"
          << "P5Q6R7S8T9"
          << "+44 7911 123456"
          << "有效"
          << "2027-06-30";
    // 研究生样例（长学号）
    QStringList data5;
    data5 << "219B01001"
          << "周晓"
          << "U7V8W9X0Y1"
          << "19912345678"
          << "有效"
          << "2028-06-30";

    // 异常状态样例（部分冻结）
    QStringList data6;
    data6 << "202305078"
          << "吴天"
          << "Z2A3B4C5D6"
          << "16600001111"
          << "部分冻结"
          << "2025-09-15";

    // 特殊字符测试
    QStringList data7;
    data7 << "G2023_002"
          << "Élève François"
          << "E7F8G9H0I1"
          << "+33 6 12 34 56 78"
          << "有效"
          << "2027-12-31";

    // 虚拟运营商号码
    QStringList data8;
    data8 << "202306123"
          << "郑小米"
          << "J3K4L5M6N7"
          << "17123456789"
          << "未激活"
          << "——";

    // 证件过期预警
    QStringList data9;
    data9 << "202307891"
          << "钱多多"
          << "O8P9Q0R1S2"
          << "13876543210"
          << "即将到期"
          << "2025-04-30";

    // 黑名单测试
    QStringList data10;
    data10 << "202308045"
           << "孙警告"
           << "T3U4V5W6X7"
           << "15210987654"
           << "黑名单"
           << "2024-12-01";

    _dataList.append(data0);
    _dataList.append(data1);
    _dataList.append(data2);
    _dataList.append(data3);
    _dataList.append(data4);
    _dataList.append(data5);
    _dataList.append(data6);
    _dataList.append(data7);
    _dataList.append(data8);
    _dataList.append(data9);
    _dataList.append(data10);

    for(QStringList &itme: _dataList)
    {
        itme.append(QString("测试数据"));
    }

    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/NaightNavigationStar.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/MaVieEnRose.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/CollapsingWorld.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/RainMan.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/DarkForest.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/Light.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/STYXHELIX.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/LASTSTARDUST.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/RunningInTheDark.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
}

R_TableViewModel::~R_TableViewModel()
{
}

int R_TableViewModel::rowCount(const QModelIndex& parent) const
{
    return _dataList.count();
}

int R_TableViewModel::columnCount(const QModelIndex& parent) const
{
    return _header.count();
}

QVariant R_TableViewModel::data(const QModelIndex& index, int role) const
{
    int row = _dataList.count();
    // int column = _header.count();
    if (role == Qt::DisplayRole && index.column() != 0)
    {
        // // 列0返回文本
        // if (index.column() == 0) {
        //     return "<书本封面>";
        // }
/*
        qDebug() << QString("_dataList[%1][%2]:%3")
                        .arg(index.row())
                        .arg(index.column() - 1)
                        .arg(_dataList[index.row()][index.column() - 1]);
*/
        return _dataList[index.row() % row][index.column() - 1];
    }
    else if (role == Qt::DecorationRole && index.column() == 0)
    {
        // return _iconList[index.row() % row];
        // return "<书本封面>";
        return QIcon(QPixmap(":/resource/Icon/table_book.png").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    else if (role == Qt::DecorationPropertyRole)
    {
        return Qt::AlignCenter;
    }
    else if (role == Qt::TextAlignmentRole /*&& index.column() == 4*/)
    {
        return Qt::AlignCenter;
    }
    return QVariant();
}

QVariant R_TableViewModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        return _header[section];
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}
